﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace EditPlatformTool
{
    public partial class fmGeneratePlatform : Form
    {
        private String _Backup = "Backup.log";
        private String _Previous = null;

        public fmGeneratePlatform()
        {
            InitializeComponent();
            LogControl.GetInstance().SetDevice(rtxtLog);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRender_Click(object sender, EventArgs e)
        {
            String path = null;

            if(ckbPUseOldDirect.Checked)
            {
                onUseOldPath(ref path);
            }
            else
            {
                onGetNewPath(ref path);
            }

            onWriteFile(path);
        }

        private void onWriteFile(String path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                String temppath = path + "\\fwPlatform.h";
                Encoding _encode = Encoding.UTF8;
                if (File.Exists(temppath))
                {
                    //Detect file encoding
                    LogControl.PrintLog(LogControl.LogType.Warning, "File " + temppath + " has already existed");
                    FileStream fo = new FileStream(temppath, FileMode.Open);
                    StreamReader sr = new StreamReader(fo);
                    _encode = sr.CurrentEncoding;
                    LogControl.PrintLog(LogControl.LogType.Info, "Detected current encode is " + _encode.EncodingName);
                    sr.Close();
                    fo.Close();
                    LogControl.PrintLog(LogControl.LogType.Info, "Deleted file " + temppath);
                    File.Delete(temppath);
                }

                //Write data
                LogControl.PrintLog(LogControl.LogType.Info, "Creating new file " + temppath);
                LogControl.PrintLog(LogControl.LogType.Info, _encode.EncodingName);
                FileStream fc = new FileStream(temppath, FileMode.Create);
                StreamWriter sw = new StreamWriter(fc, _encode);
                sw.Write(StreamData());
                sw.Close();
                fc.Close();
            }
        }

        private void onGetNewPath(ref String path)
        {
            path = FolderProcess.GetInstance().GetPath();

            //Store new directory
            if (!string.IsNullOrEmpty(path))
            {
                LogControl.PrintLog(LogControl.LogType.Info, "Write log path file");
                FileStream foc = new FileStream(_Backup, FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(foc);
                sw.WriteLine(path);
                sw.Close();
                foc.Close();
            }
            else
            {
                LogControl.PrintLog(LogControl.LogType.Error, "There are not path");
            }
        }

        private void onUseOldPath(ref String path)
        {
            if(!File.Exists(_Backup))
            {
                LogControl.PrintLog(LogControl.LogType.Error, "Log file isn't existed");
                return;
            }
            FileStream fo = new FileStream(_Backup, FileMode.Open);

            StreamReader sr = new StreamReader(fo);
            _Previous = sr.ReadLine();
            if(string.IsNullOrEmpty(_Previous))
            {
                LogControl.PrintLog(LogControl.LogType.Error, "There are not log path");
                sr.Close();
                fo.Close();
                return;
            }

            path = _Previous;

            sr.Close();
            fo.Close();
        }


        private void fmGeneratePlatform_Load(object sender, EventArgs e)
        {
            onLoadLog();
            
            lbEmulator.Text = "BlueStack";

            List<Data> platform = new List<Data>();
            platform.Add(new Data("Win32", "PLATFORM_WIN32"));
            platform.Add(new Data("Android", "PLATFORM_ANDROID"));
            cbPlatform.DataSource = platform;
            cbPlatform.DisplayMember = "View";
            cbPlatform.ValueMember = "Key";

            List<Data> device = new List<Data>();
            device.Add(new Data("PC/Laptop", "DEVICE_PC_LAPTOP"));
            device.Add(new Data("BlueStack Emulator (Android)", "DEVICE_EMULATOR_ANDROID"));
            cbDevice.DataSource = device;
            cbDevice.DisplayMember = "View";
            cbDevice.ValueMember = "Key";

            List<Data> graphics = new List<Data>();
            graphics.Add(new Data("OpenGLESv2", "GRAPHICS_LIB_OPENGLES_20"));
            cbGraphics.DataSource = graphics;
            cbGraphics.DisplayMember = "View";
            cbGraphics.ValueMember = "Key";
        }

        private static void onLoadLog()
        {
            LogControl.PrintLog(LogControl.LogType.Info, "Current directory: " + Directory.GetCurrentDirectory());
        }

        private String StreamData()
        {
            string data = "";
            string path = Directory.GetCurrentDirectory() + "\\Resources\\fwPlatform.h";

            if(!File.Exists(path))
            {
                LogControl.PrintLog(LogControl.LogType.Error, "Template file isn't exist");
                return null;
            }

            FileStream fo = new FileStream(path, FileMode.Open);
            StreamReader sr = new StreamReader(fo);

            while(!sr.EndOfStream)
            {
                char c = (char)sr.Read();
                if(c == '$')
                {
                    string key = "";
                    key += (char)sr.Read();
                    key += (char)sr.Read();
                    key += (char)sr.Read();
                    key += (char)sr.Read();
                    switch(key)
                    {
                        case "PLAT":
                            data += cbPlatform.SelectedValue;
                            break;
                        case "DEVI":
                            data += cbDevice.SelectedValue;
                            break;
                        case "GRAP":
                            data += cbGraphics.SelectedValue;
                            break;
                        default:
                            break;
                    }
                }
                else
                    data += c;
            }

            sr.Close();
            fo.Close();

            return data;
        }

        private void btnABS_NDK_Click(object sender, EventArgs e)
        {
            FolderProcess.GetInstance().Show();
        }

        private void btnPDirectory_Click(object sender, EventArgs e)
        {
            FolderProcess.GetInstance().Show();
        }

        private void ckbPUseOldDirect_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbPUseOldDirect.Checked)
                btnPDirectory.Enabled = false;
            else
                btnPDirectory.Enabled = true;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            rtxtLog.Clear();
            onLoadLog();
        }
    }
}
